using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using Activa.CoolStorage;

namespace Activa.ProMesh
{
    public static class CSProMeshProvider
    {
        public static void Configure()
        {
            string tableNameSession = "awtSession";
            string tableNameVisitor = "awtVisitor";
            string tableNameTraffic = "awtTraffic";
            string tableNamePageLog = "awtPageLog";
            string tableNameTranslation = "awtTranslation";
            string tableNameLanguage = "awtLanguage";

            foreach (string configKey in ConfigurationManager.AppSettings.Keys)
            {
                string configValue = ConfigurationManager.AppSettings[configKey];

                switch (configKey.ToLower())
                {
                    case "tablenamesession":
                        tableNameSession = configValue;
                        break;
                    case "tablenamevisitor":
                        tableNameVisitor = configValue;
                        break;
                    case "tablenamepagelog":
                        tableNamePageLog = configValue;
                        break;
                    case "tablenametraffic":
                        tableNameTraffic = configValue;
                        break;
                    case "tablenametranslation":
                        tableNameTranslation = configValue;
                        break;
                    case "tablenamelanguage":
                        tableNameLanguage = configValue;
                        break;
                }
            }

            string TableContextSession = null;
            string TableContextVisitor = null;
            string TableContextTraffic = null;
            string TableContextPageLog = null;
            string TableContextTranslation = null;
            string TableContextLanguage = null;

            if (tableNameSession.Split('.').Length > 1)
            {
                TableContextSession = tableNameSession.Split('.')[0];
                tableNameSession = tableNameSession.Split('.')[1];
            }
            if (tableNameVisitor.Split('.').Length > 1)
            {
                TableContextVisitor = tableNameVisitor.Split('.')[0];
                tableNameVisitor = tableNameVisitor.Split('.')[1];
            }
            if (tableNameTraffic.Split('.').Length > 1)
            {
                TableContextTraffic = tableNameTraffic.Split('.')[0];
                tableNameTraffic = tableNameTraffic.Split('.')[1];
            }
            if (tableNamePageLog.Split('.').Length > 1)
            {
                TableContextPageLog = tableNamePageLog.Split('.')[0];
                tableNamePageLog = tableNamePageLog.Split('.')[1];
            }
            if (tableNameTranslation.Split('.').Length > 1)
            {
                TableContextTranslation = tableNameTranslation.Split('.')[0];
                tableNameTranslation = tableNameTranslation.Split('.')[1];
            }
            if (tableNameLanguage.Split('.').Length > 1)
            {
                TableContextLanguage = tableNameLanguage.Split('.')[0];
                tableNameLanguage = tableNameLanguage.Split('.')[1];
            }

            CSConfig.ChangeTableMapping(typeof(Session), tableNameSession, TableContextSession);
            CSConfig.ChangeTableMapping(typeof(Visitor), tableNameVisitor, TableContextVisitor);
            CSConfig.ChangeTableMapping(typeof(TrafficEntry), tableNameTraffic, TableContextTraffic);
            CSConfig.ChangeTableMapping(typeof(TranslationRecord), tableNameTranslation, TableContextTranslation);
            CSConfig.ChangeTableMapping(typeof(PageLogEntry), tableNamePageLog, TableContextPageLog);
            CSConfig.ChangeTableMapping(typeof(Language), tableNameLanguage, TableContextLanguage);
        }
    }
}
